# 字符串的编码
'中国上海'.encode("unicode_escape").decode()

'\u4e2d\u56fd\u4e0a\u6d77'

# 字符串的解码
"中国上海".encode("utf-8")
b'\xe4\xb8\xad\xe5\x9b\xbd\xe4\xb8\x8a\xe6\xb5\xb7'

# 练习字符串的CRUD
>>> a = "shanghai"
>>> id(a)
2587958455216
>>> a = a + "dongfang"
>>> id(a)
2587958461328
>>> print(a)
shanghaidongfang
>>>
>>> '中国上海'.encode("utf-8")
b'\xe4\xb8\xad\xe5\x9b\xbd\xe4\xb8\x8a\xe6\xb5\xb7'
>>> b.decode("utf")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>> a = "shanghai"
>>> id(a)
2587958455216
>>> a = a + "dongfang"
>>> id(a)
2587958461328
>>> print(a)
shanghaidongfang
>>> a[3]
'n'
>>> a.find("a")
2
>>> a.find("m")
-1
>>> a.startwith("\nzhongguo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'startwith'
>>> a.startswith("\nzhongguo")
False
>>> a.startswith("zhongguo")
False
>>> m = "zhongguo-shanghai-dongfang"
>>> a.startswith("zhongguo")
False
>>> m.startswith("zhongguo")
True
>>> print m
  File "<stdin>", line 1
    print m
          ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(m)?
>>> print(m)
zhongguo-shanghai-dongfang
>>> m.endswith("dongfang")
True
>>> m.replace("shanghai", "zhejiang")
'zhongguo-zhejiang-dongfang'
>>> m.split(",")
['zhongguo-shanghai-dongfang']
>>> m = ("zhongguo", "shanghai", "dongfang")
>>> m.split(",")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'split'
>>> m = "zhongguo, shanghai, dongfang"
>>> m.split(",")
['zhongguo', ' shanghai', ' dongfang']
>>> ",".join(m)
'z,h,o,n,g,g,u,o,,, ,s,h,a,n,g,h,a,i,,, ,d,o,n,g,f,a,n,g'
>>> print(m)
zhongguo, shanghai, dongfang
>>> m.strip()
'zhongguo, shanghai, dongfang'
>>> print(m)
zhongguo, shanghai, dongfang
>>>
# 练习字符串的格式化
>>> g = "shang"
>>> h = "hai"
>>> "go to shanhai:{},{}.format(a,b)
  File "<stdin>", line 1
    "go to shanhai:{},{}.format(a,b)
                                   ^
SyntaxError: EOL while scanning string literal
>>> "stay shanhai:{},{}.format(a,b)
  File "<stdin>", line 1
    "stay shanhai:{},{}.format(a,b)
                                  ^
SyntaxError: EOL while scanning string literal
>>> "play shanghai:{}, {}".format(a,b)
'play shanghai:shanghaidongfang, ä¸\xadå\x9b½ä¸\x8aæµ·'
>>> "play shanghai:{}, {}".format(g, h)
'play shanghai:shang, hai'
>>> "go to shanghai:{}{}".format(a,b)
'go to shanghai:shanghaidongfangä¸\xadå\x9b½ä¸\x8aæµ·'
>>> "go to shanghai:{}{}".format(g,h)
'go to shanghai:shanghai'
>>> "go to shanghhai:{0}{0}{1}{1}”。format(g, h)
  File "<stdin>", line 1
    "go to shanghhai:{0}{0}{1}{1}”。format(g, h)
                                               ^
SyntaxError: EOL while scanning string literal
>>> "go to shanghhai:{0}{0}{1}{1}”.format(g, h)
  File "<stdin>", line 1
    "go to shanghhai:{0}{0}{1}{1}”.format(g, h)
                                               ^
SyntaxError: EOL while scanning string literal
>>> "go to shanghhai:{0},{0},{1},{1}”.format(g, h)
  File "<stdin>", line 1
    "go to shanghhai:{0},{0},{1},{1}”.format(g, h)
                                                  ^
SyntaxError: EOL while scanning string literal
>>> "go to shanghhai: {0}, {0}, {1}, {1}”.format(g, h)
  File "<stdin>", line 1
    "go to shanghhai: {0}, {0}, {1}, {1}”.format(g, h)
                                                      ^
SyntaxError: EOL while scanning string literal
>>> print(g)
shang
>>> "go to shanghhai: {0}, {1}, {0}, {1}”.format(g, h)
  File "<stdin>", line 1
    "go to shanghhai: {0}, {1}, {0}, {1}”.format(g, h)
                                                      ^
SyntaxError: EOL while scanning string literal
>>> "go to shanghai: {0}, {1}, {0}, {1}”.format(g, h)
  File "<stdin>", line 1
    "go to shanghai: {0}, {1}, {0}, {1}”.format(g, h)
                                                     ^
SyntaxError: EOL while scanning string literal
>>> a= "ping"
>>> b = "pang"
>>> "play pingpong: {0},{1},{0},{1}".format(a, b)
'play pingpong: ping,pang,ping,pang'
>>> "play pingpong: {0}, {1},{0}, {1}".format(g,h)
'play pingpong: shang, hai,shang, hai'
>>> print("go to shanghai: {g}, {h}, {g}, {h}".format=(g='shang',h='hai'))
  File "<stdin>", line 1
    print("go to shanghai: {g}, {h}, {g}, {h}".format=(g='shang',h='hai'))
                                                        ^
SyntaxError: invalid syntax
>>> print("go to shanghai: {g}, {h}, {g}, {h}".format(g='shang',h='hai'))
go to shanghai: shang, hai, shang, hai
>>> print(f"go to shanghai: {g},{h}")
go to shanghai: shang,hai
>>> "{:.2f}".format(5.122325425)
'5.12'
>>> "{:.4f}".format(5.122325425)
'5.1223'
>>> "go to %s %s" % ("ping", "pong")
'go to ping pong'

# 将content内容保存到本地文件
>>> output = open("output.txt", "w", encoding="utf-8")
>>> content = "go to shanghai"
>>> output.write(content)
14
>>> output.close()
>>>
>>> output = open("shanghai.txt", "w", encoding="utf-8")
>>> content = "go to shanghai"
>>> output.write(content)
14
>>> output.close()
>>> input = open("shanghai.txt", "r", encoding= "utf-8")
>>> content = input.read()
>>> print(content)
go to shanghai
>>> content = "\nI love shanghai"
>>> output.write(content)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
>>> output = open("shanghai.txt", "a", encoding="utf-8")
>>> content = "\nI love shanghai"
>>> output.write(content)
16
>>> output.close()
>>> input = open("shanghai.txt", "r", encoding= "utf-8")
>>> content = input.read()
>>> print(content)
go to shanghai
I love shanghai
>>>
